uniform sampler2D tex, normalMap, heightMap;
varying vec2 texcoord;
varying vec4 eyeDir;
varying vec4 lightDir, halfAngle;
varying vec4 lightDir2, halfAngle2;
varying float att, att2;
//varying float test;

void calcParallax(in vec4 eyeDir, inout vec2 texcoord)
{
   float height = texture2D(heightMap, texcoord).r * 0.04 - 0.02;
   texcoord += (eyeDir.xy * height);
}

float calcDiffuseLight(in vec4 normal, in vec4 alightDir)
{
   return max( 0.0, dot( normal, alightDir ) );
}

float calcSpecularLight(in vec4 normal, in vec4 ahalfAngle)
{
   return pow( clamp( dot( normal, ahalfAngle ), 0.0, 1.0 ), 32 );
}

float calcSumLight(in float diffuse, in float specular)
{
   /*if ( diffuse > 0 )
   {
		return diffuse + specular;
   }
   else
   {
		return 0;
   }*/

   return diffuse + specular;
   //return 1;
}

void main()
{
   eyeDir = normalize( eyeDir );

   lightDir = normalize( lightDir );
   halfAngle = normalize( halfAngle );
   lightDir2 = normalize( lightDir2 );
   halfAngle2 = normalize( halfAngle2 );

   calcParallax( eyeDir, texcoord );

   vec3 normal2 = normalize(texture2D(normalMap, texcoord).rgb * 2.0 - 1.0);
   vec4 normal4f = vec4( normal2, 0 );
   //float intensity = ((calcDiffuseLight( normal4f, lightDir ) + calcSpecularLight( normal4f, halfAngle )) * att)
	//		+ ((calcDiffuseLight( normal4f, lightDir2 ) + calcSpecularLight( normal4f, halfAngle2 )) * att2); 

   float intensity = (( calcSumLight(calcDiffuseLight( normal4f, lightDir ), calcSpecularLight( normal4f, halfAngle )) ) * att)
			+ (( calcSumLight(calcDiffuseLight( normal4f, lightDir2 ), calcSpecularLight( normal4f, halfAngle2 )) ) * att2); 

   gl_FragColor = texture2D(tex, texcoord) * intensity;
   //gl_FragColor = vec4(test, test, test, test);
}
